// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/dwio/common/writer.h>

namespace kumo::pollux::dwio::common {
    void Writer::checkStateTransition(State oldState, State newState) {
        switch (oldState) {
            case State::kInit:
                if (newState == State::kRunning) {
                    return;
                }
                break;
            case State::kRunning:
                // NOTE: some physical file writer might switch from kRunning to kClosed
                // directly as there is nothing to do with finish() call.
                if (newState == State::kAborted || newState == State::kClosed ||
                    newState == State::kFinishing) {
                    return;
                }
                break;
            case State::kFinishing:
                if (newState == State::kAborted || newState == State::kClosed ||
                    // NOTE: the finishing state is reentry state as we could yield in the
                    // middle of long running finish processing.
                    newState == State::kFinishing) {
                    return;
                }
                break;
            case State::kAborted:
                [[fallthrough]];
            case State::kClosed:
                [[fallthrough]];
            default:
                break;
        }
        POLLUX_FAIL(
            "Unexpected state transition from {} to {}",
            Writer::stateString(oldState),
            Writer::stateString(newState));
    }

    std::string Writer::stateString(State state) {
        switch (state) {
            case State::kInit:
                return "INIT";
            case State::kRunning:
                return "RUNNING";
            case State::kFinishing:
                return "FINISHING";
            case State::kClosed:
                return "CLOSED";
            case State::kAborted:
                return "ABORTED";
            default:
                POLLUX_UNREACHABLE("BAD STATE: {}", static_cast<int>(state));
        }
    }

    bool Writer::isRunning() const {
        return state_ == State::kRunning;
    }

    bool Writer::isFinishing() const {
        return state_ == State::kFinishing;
    }

    void Writer::checkRunning() const {
        POLLUX_CHECK_EQ(
            state_,
            State::kRunning,
            "Writer is not running: {}",
            Writer::stateString(state_));
    }

    void Writer::setState(State state) {
        checkStateTransition(state_, state);
        state_ = state;
    }
} // namespace kumo::pollux::dwio::common
